<template>
  <div class="upload-container flex-container">
      <input type="file" class="hide-upload-input" @change="onChange" ref="upload-input">
    <div class="title">
        {{title}} <span v-if="!!title">:</span>
    </div>
    <div class="content" :style="conTop">
        <div class="img-upload" @click="postFile">
            <div class="camera">
                <img class="extend" src="http://cdn-ali-img-shdiy.shanhutech.cn/wlsm/assets/camera.png" alt="">
            </div>  
            <div class="tips">
                点击上传营业执照
            </div>
        </div>
        <div class="error">
        </div>  
    </div>
  </div>
</template>

<script>
export default {
    name:'upload-file',
    data(){
        return{
            getFileTyle:['png','jepg','jpg'],
            conTop: {
                    backgroundImage:'',
                    backgroundRepeat:'no-repeat',
                    backgroundSize: 'cover'
            }
        }
    },
    methods:{
        onChange(event){
            var e = window.event || event
            var file = e.target.files[0]
            e.target.value = ''
            if(!!file){
                let fileType = file.name.substr(file.name.lastIndexOf('.')+1).toLocaleLowerCase()
                // let fileSize = file.size()
                if(this.getFileTyle.indexOf(fileType) === -1){
                    console.log('error file type-' + file.name)
                    return
                }
                this.$emit("change",{file:file})
            }
            this.conTop.backgroundImage = 'url(' + window.URL.createObjectURL(file) + ')'
        },
        postFile(){
            this.$refs['upload-input'].click()
        }
    },
    props:{
        title:{},
    }
}
</script>

<style lang="less" scoped>
.extend{
    width: 100%;
    height: 100%;
    display: block;
}
.upload-container{
    align-items: flex-start !important;
    .hide-upload-input{
        position: absolute;
        z-index: -1;
    }
}
.content{
    width: 614px;
}

.flex-container{
    display: flex;
}
.title{
    width: 200px;
    text-align: right;
    font-size: 20px;
    font-family: Alibaba PuHuiTi;
    font-weight: 400;
    color: #474747;
    box-sizing: border-box;
    padding: 5px 0;
    padding-right: 20px;
}
.content{
    width: 414px;
    height: 209px;
    background: #F8E9C9;
    border: 1px solid #333333;
    border-radius: 4px;
    position: relative;
    .img-upload{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        .camera{
            width: 44px;
            height: 39px;
            margin: 0 auto;
            margin-bottom: 20px;
            cursor: pointer;
        }
        .tips{
            font-size: 20px;
            font-family: Alibaba PuHuiTi;
            font-weight: 400;
            color: #575757;
        }
    }
}

</style>